﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using gds_classes;

namespace gds_verify
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("GOST Sign Checker");
            Console.WriteLine("Enter message: ");
            int m = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter r: ");
            int r = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter s: ");
            int s = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter open key (y): ");
            int y = int.Parse(Console.ReadLine());

            if (r < 0 || r > GOST_Signification.q || s < 0 || s > GOST_Signification.q)
            {
                Console.WriteLine("Sign check fail!");
                return;
            }

            int h = GOST_Signification.Hash(m);
            //int invHmodQ = MathMethods.FindInversion(h, GOST_Signification.q);

            int h_inv = MathMethods.FindInversion(h, GOST_Signification.q);

            int u1 = (s * h_inv) % GOST_Signification.q;
            int u2 = MathMethods.Mod((-r * h_inv), GOST_Signification.q);

            var v = MathMethods.Mod(MathMethods.Mod(MathMethods.PowByMod(GOST_Signification.a, u1, GOST_Signification.p) * MathMethods.PowByMod(y, u2, GOST_Signification.p), GOST_Signification.p), GOST_Signification.q);

            if (r != v)
            {
                Console.WriteLine("Sign check fail");
            }
            else
            {
                Console.WriteLine("Sign check ok!");
            }

            Console.ReadLine();
        }
    }
}
